# Copyright (C) 2011 David Sugar
#
# This file is free software; as a special exception the author gives
# unlimited permission to copy and/or distribute it, with or without
# modifications, as long as this notice is preserved.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
cmake_minimum_required(VERSION 2.6)
PROJECT("GNU Telephony")

include(FindPkgConfig)
include(CheckIncludeFiles)
include(vcs.cmake)

set(BUILD_PACKAGE OFF CACHE BOOL "disable individual packaging" FORCE)
MARK_AS_ADVANCED(FORCE BUILD_PACKAGE)
option(BUILD_STDLIB "Set to ON to build with C++ stdlib" OFF)
option(BUILD_COMPAT "Set to ON to enable Common C++ compatibility" OFF)
option(BUILD_MINISHELL "Set to ON to build minishell utils" OFF)

if (NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Debug CACHE STRING "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel." FORCE)
endif ()

if(WIN32)
    set(CMAKE_INSTALL_PREFIX "C:/Testing" CACHE STRING "Choose install prefix.")
    set(BUILD_STATIC ON CACHE BOOL "static linking only" FORCE)
    MARK_AS_ADVANCED(BUILD_STATIC)
else()
    set(CMAKE_INSTALL_PREFIX "/usr/testing" CACHE STRING "Choose install prefix.")
endif()

message(STATUS "Configure project GNU Telephony")

if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../frameworks")
    file(TO_CMAKE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../frameworks" LOCAL_FRAMEWORKS)
    file(TO_CMAKE_PATH "${LOCAL_FRAMEWORKS}/include" LOCAL_INCLUDES)
    file(TO_CMAKE_PATH "${LOCAL_FRAMEWORKS}/${CMAKE_BUILD_TYPE}" LOCAL_LIBS)
    set(CMAKE_REQUIRED_INCLUDES ${LOCAL_INCLUDES} ${CMAKE_REQUIRED_INCLUDES})
    link_directories(${LOCAL_LIBS})
    include_directories(${LOCAL_INCLUDES})
endif()

if(MSVC60)
    set(BUILD_QT false)
    set(BUILD_MINIMAL true)
    set(BUILD_RUNTIME_TYPE STATIC)
    message(STATUS "Building with minimal support")
else()
    set(BUILD_MINIMAL false)
    option(BUILD_RUNTIME "Set to OFF for static runtime" ON)

    if(BUILD_RUNTIME)
        set(BUILD_RUNTIME_TYPE SHARED)
    endif()

    if(LOCAL_FRAMEWORKS AND EXISTS "${LOCAL_FRAMEWORKS}/qt")
        file(TO_CMAKE_PATH "${LOCAL_FRAMEWORKS}/qt" LOCAL_QT)
        set(CMAKE_PREFIX_PATH ${LOCAL_QT} ${CMAKE_PREFIX_PATH})
    endif()

    find_package(Qt4)
    if(QT4_FOUND)
        set(BUILD_QT true)
        message(STATUS "Building with QT Support")
    else()
        set(BUILD_QT false)
        message(STATUS "Building without QT")
    endif()
endif()

git_checkout(ucommon git@github.com:dyfet/ucommon.git
    ssh://git.sv.gnu.org/srv/git/commoncpp.git)

include(${CMAKE_SOURCE_DIR}/ucommon/inc/ucommon.cmake)
set(USES_UCOMMON_CFLAGS ${UCOMMON_FLAGS})
set(USES_UCOMMON_LIBRARIES ucommon)
set(USES_UCOMMON_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/ucommon/inc)
set(USES_CCRTP_CFLAGS ${UCOMMON_FLAGS})
set(USES_CCRTP_LIBRARIES ccrtp commoncpp ucommon)
set(USES_CCRTP_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/ucommon/inc ${CMAKE_SOURCE_DIR}/ccrtp/src)

add_subdirectory(ucommon)

if(BUILD_COMPAT AND BUILD_STDLIB)
    git_subdirectory(ccrtp git@github.com:dyfet/ccrtp.git
        ssh://git.sv.gnu.org/srv/git/ccrtp.git)
    check_include_files(openssl/ssl.h HAVE_OPENSSL)
    pkg_check_modules(OPENSSL_FOUND libcrypto>=0.9.8)
    if (OPENSSL_FOUND)
        git_subdirectory(libzrtpcpp git@github.com:dyfet/ZRTPCPP.git
            git@github.com:wernerd/ZRTPCPP.git)
    else()
        git_checkout(libzrtpcpp git@github.com:dyfet/ZRTPCPP.git
            git@github.com:wernerd/ZRTPCPP.git)
    endif()
else()
    # if not currently used, checkout in case later configured....
    git_checkout(ccrtp git@github.com:dyfet/ccrtp.git
        ssh://git.sv.gnu.org/srv/git/ccrtp.git)
    git_checkout(libzrtpcpp git@github.com:dyfet/ZRTPCPP.git
            git@github.com:wernerd/ZRTPCPP.git)
endif()

svn_subdirectory(vendor https://gnutelephony.googlecode.com/svn/trunk/cmake/vendor)
set(CMAKE_REQUIRED_INCLUDES ${CMAKE_REQUIRED_INCLUDES} ${CMAKE_SOURCE_DIR}/vendor/osip2/include ${CMAKE_SOURCE_DIR}/vendor/exosip2/include ${CMAKE_SOURCE_DIR}/vendor/ortp/include ${CMAKE_SOURCE_DIR}/vendor/include)
include_directories(${CMAKE_REQUIRED_INCLUDES})

git_subdirectory(bayonne git@github.com:dyfet/bayonne
    ssh://git.sv.gnu.org/srv/git/bayonne.git)

git_subdirectory(apennine git@github.com:dyfet/apennine)

if(BUILD_MINISHELL)
    git_subdirectory(minishell git@github.com:dyfet/cape-minishell)
else()
    git_checkout(minishell git@github.com:dyfet/cape-minishell)
endif()

if(NOT BUILD_MINIMAL)
    set(USES_SIPWITCH_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/sipwitch/inc)
    git_subdirectory(sipwitch git@github.com:dyfet/sipwitch.git
        ssh://git.sv.gnu.org/srv/git/sipwitch.git)
endif()

# we will only use it for git checkouts...
if(EXISTS ".git")
    add_custom_target(update
        COMMAND ${CMAKE_SOURCE_DIR}/update.sh
            "ucommon sipwitch apennine bayonne ccrtp libzrtpcpp"
        WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
        COMMENT "Update checkouts")
endif()

find_package(Doxygen)

if (DOXYGEN_FOUND)
    configure_file(${CMAKE_CURRENT_SOURCE_DIR}/doxy.config.cmake
      ${CMAKE_CURRENT_BINARY_DIR}/doxy.config
      @ONLY )

    set(DOXY_CONFIG "${CMAKE_CURRENT_BINARY_DIR}/doxy.config")

    #add_custom_target(apidocs ${DOXYGEN_EXECUTABLE} ${DOXY_CONFIG})
    add_custom_target(apidocs
                    ${DOXYGEN_EXECUTABLE} ${DOXY_CONFIG}
                    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
                    COMMENT "Generating API documentation with Doxygen" VERBATIM
                   )

    #
    # TODO an: taken from internet, to be tested
    #
    # create a windows help .chm file using hhc.exe
    # HTMLHelp DLL must be in path!
    # fallback: use hhw.exe interactively
    IF (WIN32)
        FIND_PACKAGE(HTMLHelp)
        IF (HTML_HELP_COMPILER)
            SET (TMP "${CMAKE_CURRENT_BINARY_DIR}\\Doc\\html\\index.hhp")
            STRING(REGEX REPLACE "[/]" "\\\\" HHP_FILE ${TMP} )
            # MESSAGE(SEND_ERROR "DBG  HHP_FILE=${HHP_FILE}")
            ADD_CUSTOM_TARGET(apiwinhelp ${HTML_HELP_COMPILER} ${HHP_FILE})
            ADD_DEPENDENCIES (apiwinhelp apidocs)

            IF (NOT TARGET_APIDOCS_SKIP_INSTALL)
            # install windows help?
            # determine useful name for output file
            # should be project and version unique to allow installing
            # multiple projects into one global directory
            IF   (EXISTS "${PROJECT_BINARY_DIR}/Doc/html/index.chm")
                IF (PROJECT_NAME)
                    SET(OUT "${PROJECT_NAME}")
                ELSE (PROJECT_NAME)
                    SET(OUT "Documentation") # default
                ENDIF(PROJECT_NAME)
                IF (${PROJECT_NAME}_VERSION_MAJOR)
                    SET(OUT "${OUT}-${${PROJECT_NAME}_VERSION_MAJOR}")
                    IF   (${PROJECT_NAME}_VERSION_MINOR)
                        SET(OUT  "${OUT}.${${PROJECT_NAME}_VERSION_MINOR}")
                        IF (${PROJECT_NAME}_VERSION_PATCH)
                            SET(OUT "${OUT}.${${PROJECT_NAME}_VERSION_PATCH}")
                        ENDIF(${PROJECT_NAME}_VERSION_PATCH)
                    ENDIF(${PROJECT_NAME}_VERSION_MINOR)
                ENDIF(${PROJECT_NAME}_VERSION_MAJOR)
                # keep suffix
                SET(OUT  "${OUT}.chm")

                #MESSAGE("DBG ${PROJECT_BINARY_DIR}/Doc/html/index.chm \n${OUT}")
                # create target used by install and package commands
                INSTALL(FILES "${PROJECT_BINARY_DIR}/Doc/html/index.chm"
                    DESTINATION ${INSTALL_DOCDIR} RENAME "${OUT}")
                ENDIF(EXISTS "${PROJECT_BINARY_DIR}/Doc/html/index.chm")
            ENDIF(NOT TARGET_APIDOCS_SKIP_INSTALL)
        ENDIF(HTML_HELP_COMPILER)
    # MESSAGE(SEND_ERROR "HTML_HELP_COMPILER=${HTML_HELP_COMPILER}")
    ENDIF (WIN32)
ENDIF(DOXYGEN_FOUND)

# message ("${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}")
